<?php
require("phpsqlajax_dbinfo.php");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Document sans nom</title>
<style type="text/css">
<!--
.Style1 {font-size: 36px}
.Style3 {font-size: 14px}
-->
</style>
</head>

<body>
<div align="center" class="Style1">
  <p><a href="phpsqlajax_map.htm">Geolocalip</a></p>
  <p>&nbsp;</p>
  <p class="Style3">service de geolocalisation de visiteur de site Web </p>
</div>
      
</body>
<?php
 
mysql_connect("localhost", $username, $password);
mysql_select_db($database);

//$reponse = mysql_query("SELECT * FROM ip");

function getlocation($ip,$date) { 
	//commandes SQL
// $reponse= mysql_query("SELECT adresse FROM ip WHERE adresse='$ip'");
//$tabereponse=mysql_fetch_array($reponse);
//if(count($tabereponse)>1){	
	//$date = time(); 
	//echo $date;
	//mysql_query("UPDATE ip SET date='$date' WHERE adresse='$ip'");
	//echo "MAJ OK";
//}else{
  $locationurl = "http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesLatitude,GeobytesLongitude,GeobytesCity,GeobytesIpAddress&geobyteslocation=".$ip; //concat de l'url et IP 
  
  if($locationf = fopen($locationurl,r)) { 
    ob_start();
    fpassthru($locationf);
    $locationhtml = ob_get_contents();
    ob_end_clean();
    fclose($locationf);
  }
echo "<br />";
if(ereg("sGeobytesLatitude=\"(.*)\";var sGeobytesLongitude",$locationhtml,$lat)) 
 $location[0] = $lat[1];
 if(ereg("sGeobytesLongitude=\"(.*)\";var sGeobytesIpAddress",$locationhtml,$long)) 
 $location[1] = $long[1];
  if(ereg("sGeobytesCountry=\"(.*)\";var sGeobytesCity",$locationhtml,$country)) 
 $location[2] = $country[1];
   if(ereg("sGeobytesCity=\"(.*)\";var sGeobytesLatitude",$locationhtml,$city)) 
 $location[3] = $city[1];
//$date = time(); 
/*
echo $ip;
echo "<br />"; 
echo $date;
echo "<br />"; 
echo "<pre>";
print_r($location);
echo "</pre>";
*/
//echo $ip."<br/>";
//echo $location[0]."<br/>";
//echo $location[1]."<br/>";
//echo $location[2]."<br/>";
//echo $location[3]."<br/>";
//echo $date."<br/>";
mysql_query("INSERT INTO ip VALUES('', '$ip', '$date', '$location[0]', '$location[1]', '$location[2]', '$location[3]')");
}


?>